package com.augurit.gzcgwsp.csshlj.web;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.script.ScriptEngine;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jbpm.api.JbpmException;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.history.HistoryTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.BaseAction;
import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.common.util.CommonUtils;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService;
import com.augurit.gzcgwsp.printDoc.web.form.YwPrintDocForm;
import com.augurit.gzcgwsp.wfex.app.doc.IOaDocCodeServiceEX;
import com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX;
import com.augurit.gzcgwsp.wfex.bi.service.IWfTemplateAttachmentRefServiceEX;
import com.augurit.gzcgwsp.wfex.bi.web.form.WfTemplateAttachmentRefFormEX;
import com.augurit.gzcgwsp.wfex.common.service.IWfServiceEX;
import com.augurit.gzcgwsp.wfex.common.util.WfUtilsEX;
import com.augurit.smartform.factory.ScriptEngineFactory;
import com.augurit.smartform.service.IFormExecuteService;
import com.augurit.smartform.service.IWfSupportService;
import com.augurit.smartform.test.TestService;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.app.doccode.web.form.OaDocCodeForm;
import com.augurit.wf.bi.convert.WfBusInstanceConvertor;
import com.augurit.wf.bi.service.IWfBusInstanceService;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfActElemPrivDefForm;
import com.augurit.wf.bi.web.form.WfBusInstanceForm;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.client.summary.web.form.WfTaskSummaryForm;
import com.augurit.wf.common.constant.WfConstant;
import com.augurit.wf.common.entity.WfBusInstance;
import com.augurit.wf.common.form.TraceRecord;
import com.augurit.wf.common.form.WfBusBaseEntity;
import com.augurit.wf.common.form.WfBusSaveResult;
import com.augurit.wf.common.service.IWfBusClientService;
import com.augurit.wf.common.service.IWfService;
import com.augurit.wf.common.util.WfUtils;
import com.augurit.wf.common.web.WfBusClientExtAction;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.gzcgwsp.ywcmn.entity.YwComQyxx;
import com.augurit.gzcgwsp.csshlj.service.ICsshljCzService;
import com.augurit.gzcgwsp.csshlj.service.IShljqsBaService;
import com.augurit.gzcgwsp.csshlj.entity.CsshljczBa;
import com.augurit.gzcgwsp.csshlj.form.CsshljczBaForm;
import com.augurit.gzcgwsp.csshlj.service.ICsshljczBaService;
import com.augurit.gzcgwsp.csshlj.web.form.CsshljCzForm;

/**
 * 从事陆域城市生活垃圾经营性清扫、收集、运输服务许可审批备案
 * @author Administrator
 *
 */
@SuppressWarnings( { "serial", "unchecked" })
@Results( {
		@Result(name ="printViewZs", location ="/gzcgwsp/shljqs/preViewXkz.jsp"),
		@Result(name ="printViewfb", location ="/gzcgwsp/shljqs/preViewfb.jsp"),
		@Result(name = CrudActionSupport.RELOAD, location = "shljqs-ba.action", type = "redirect"),
		@Result(name = WfConstant.PRINT, location = WfConstant.PRINT_JSP),
		@Result(name = WfConstant.PRINT_DIR, location = WfConstant.PRINT_DIR_JSP),
		@Result(name = CrudActionSupport.INPUT, location = "/gzcgwsp/shljcz/ShljczBa-input.jsp"),
		@Result(name = "printxkzs", location = "/gzcgwsp/wfex/client/wf-printExcelEx.jsp"),
		@Result(name = "printfb", location = "/gzcgwsp/wfex/client/wf-printExcelEx.jsp"),
		@Result(name = WfConstant.MAKE_DOCUMENT, location = "/gzcgwsp/wfex/client/common/make-rqjyxk-document.jsp") })
public class CsshljczBaAction extends WfBusClientExtAction {
	private static final Logger log=Logger.getLogger(CsshljczBaAction.class);
	@Autowired
	private TestService testService;

	@Autowired
	private IWfSupportService wfSupportService;

	@Autowired
	private IFormExecuteService formExecuteService;

	@Autowired
	private IOaDocCodeService docCodeService;

	@Autowired
	private IOaDocCodeServiceEX oaDocCodeServiceEX;

	@Autowired
	private IWfBusInstanceService wfBusInstanceService;

	@Autowired
	private IWfTemplateAttachmentRefServiceEX wfTemplateAttachmentRefServiceEX;

	@Autowired
	private IWfBusTemplateServiceEX wfBusTemplateServiceEX;

    @Autowired
    private IWfBusTemplateService wfBusTemplateService;

    @Autowired
    private IWfServiceEX wfServiceEX;
	
    @Autowired
    private ProcessEngine processEngine;
	@Autowired
	private IYwPrintDocService ywPrintDocService;

	@Autowired
	private IShljqsBaService shljqsBaService;
	
	@Autowired
	private ICsshljCzService csshljCzService;

	@Autowired
	private ICsshljczBaService csshljczBaService;

	private Page<CsshljczBaForm> page = new Page<CsshljczBaForm>(10);
	private Page<CsshljCzForm> qspage = new Page<CsshljCzForm>(10);
	private Map pageMap = new HashMap();

	private Long id;
	private CsshljczBaForm form;

    /**
     * 保存业务并关联流程实例
     */
    public String wfBusSave() {
    	try{
    		if(!StringUtils.isBlank(form.getQymc())){
    			form.setSqdw(form.getQymc());
    		}
    		WfBusInstanceTaskForm task = this.wfBusSave((IWfBusClientService)csshljczBaService, form, CsshljczBa.class.getSimpleName());
    		WfUtils.renderWfBusSaveJson(task, form);
    	}catch (Exception e) {
      		WfUtils.renderWfFailtureJson("请检查输入内容长度是否过大!");
   		}
        return null;
    }

    public String dataList() throws Exception {
        ExtUtils.initPageFromExtGridParam(getRequest(), qspage);
        CsshljCzForm qsForm = new CsshljCzForm();
        if(form !=null && form.getSqdw()!=null){
        	qsForm.setSqdw(form.getSqdw());
        }
        csshljCzService.search(qspage, qsForm);
		
        renderText(Struts2Utils.getJsonString(qspage, DateUtils.YEAR_MOUTH_DAY_));
        return null;
    }

	/**
	 * 获得办结ID
	 */
    public String getBj() {
    	try{
    		if(form.getCzid()!=null){
    			WfTaskSummaryForm bjform = null ;// 建立新的分页对象
    			bjform = shljqsBaService.getBjsj(form.getCzid().toString(), "shljcz");
    			if(bjform.getWfBusInstanceId()!=null){
                    getResponse().getWriter().print("exist"+bjform.getWfBusInstanceId());
    			}
    		}
    	}catch (Exception e) {
   		}
        return null;
    }
	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {

		return INPUT;
	}
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {			
		if(id!=null){
			form = csshljczBaService.get(id);
		}else{
			form = new CsshljczBaForm();
		}
		if (form.getSqrq() == null) {
			form.setSqrq(new Date());
		}
		if (StringUtils.isBlank(form.getCbyw())) {
            WfBusTemplateForm wfBusTemplateForm = wfBusTemplateService.getByTemplateCode(getTemplateCode());
            if (wfBusTemplateForm != null) {
                form.setCbyw(wfBusTemplateForm.getTemplateName());
            }
        }
		
		if (StringUtils.isBlank(form.getSjr())) {
            form.setSjr(this.getLoginUser().getUser().getUserName());
        }
		
		if (StringUtils.isBlank(form.getCbbm())) {
            form.setCbbm("环卫处");
        }
        try {
            if (form.getSarq() == null) {
                form.setSarq(new Date());
            }
            if (form.getSqrq() == null) {
                form.setSqrq(new Date());
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
	}
	
	

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		csshljczBaService.save(form);
		return RELOAD;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CsshljczBaForm getModel() {
		// TODO Auto-generated method stub
		return form;
	}

	public Page<CsshljczBaForm> getPage() {
		return page;
	}

	public void setPage(Page<CsshljczBaForm> page) {
		this.page = page;
	}

}
